<!DOCTYPE html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>HTMLImageElement.prototype.decode(), basic tests.</title>
<link rel="author" title="Vladimir Levin" href="mailto:vmpstr@chromium.org">
<link rel=help href="https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
"use strict";

// src tests
// -------------------
promise_test(function() {
  var img = new Image();
  img.src = "/images/green.png";
  return img.decode().then(function(arg) {
    assert_equals(arg, undefined);
  });
}, document.title + " Image with PNG src decodes with undefined.");

promise_test(function() {
  var img = new Image();
  img.src = "" +
            "D91JpzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QUSEioKsyAgyw" +
            "AAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAWSURBVA" +
            "jXY9y3bx8DAwPL58+fGRgYACktBRltLfebAAAAAElFTkSuQmCC";
  return img.decode().then(function(arg) {
    assert_equals(arg, undefined);
  });
}, document.title + " Image with PNG data URL src decodes with undefined.");

promise_test(function() {
  var img = new Image();
  img.src = "/images/green.svg";
  return img.decode().then(function(arg) {
    assert_equals(arg, undefined);
  });
}, document.title + " Image with SVG src decodes with undefined.");

promise_test(function(t) {
  var img = new Image();
  img.src = "/non/existent/path.png";
  var promise = img.decode();
  return promise_rejects_dom(t, "EncodingError", promise);
}, document.title + " Non-existent src fails decode.");

promise_test(function(t) {
  var inactive_doc = document.implementation.createHTMLDocument();
  var img = inactive_doc.createElement("img");
  img.src = "/images/green.png";
  var promise = img.decode();
  return promise_rejects_dom(t, "EncodingError", promise);
}, document.title + " Inactive document fails decode.");

promise_test(function(t) {
  var inactive_doc = document.implementation.createHTMLDocument();
  var img = document.createElement("img");
  img.src = "/images/green.png";
  var promise = img.decode();
  inactive_doc.body.appendChild(img);
  return promise_rejects_dom(t, "EncodingError", promise);
}, document.title + " Adopted active image into inactive document fails decode.");

promise_test(function() {
  var inactive_doc = document.implementation.createHTMLDocument();
  var img = inactive_doc.createElement("img");
  img.src = "/images/green.png";
  document.body.appendChild(img);
  return img.decode().then(function(arg) {
    assert_equals(arg, undefined);
  });
}, document.title + " Adopted inactive image into active document succeeds.");

promise_test(function(t) {
  var img = new Image();
  img.src = "";
  var promise = img.decode();
  return promise_rejects_dom(t, "EncodingError", promise);
}, document.title + " Corrupt image in src fails decode.");

promise_test(function(t) {
  var img = new Image();
  var promise = img.decode();
  return promise_rejects_dom(t, "EncodingError", promise);
}, document.title + " Image without src/srcset fails decode.");

promise_test(function() {
  var img = new Image();
  img.src = "/images/green.png";
  var first_promise = img.decode();
  var second_promise = img.decode();
  assert_not_equals(first_promise, second_promise);
  return Promise.all([
    first_promise,
    second_promise
  ]);
}, document.title + " Multiple decodes for images with src succeed.");

// srcset tests
// -------------------
promise_test(function() {
  var img = new Image();
  img.srcset = "/images/green.png 100w";
  return img.decode().then(function(arg) {
    assert_equals(arg, undefined);
  });
}, document.title + " Image with PNG srcset decodes with undefined.");

promise_test(function() {
  var img = new Image();
  img.srcset = "/images/green.svg 100w";
  return img.decode().then(function(arg) {
    assert_equals(arg, undefined);
  });
}, document.title + " Image with SVG srcset decodes with undefined.");

promise_test(function(t) {
  var img = new Image();
  img.srcset = "/non/existent/path.png 100w";
  var promise = img.decode();
  return promise_rejects_dom(t, "EncodingError", promise);
}, document.title + " Non-existent srcset fails decode.");

promise_test(function() {
  var img = new Image();
  img.srcset = "/images/green.png 100w";
  var first_promise = img.decode();
  var second_promise = img.decode();
  assert_not_equals(first_promise, second_promise);
  return Promise.all([
    first_promise,
    second_promise
  ]);
}, document.title + " Multiple decodes for images with srcset succeed.");

</script>
